superstrict

import BRL.StandardIO
import BRL.PolledInput
import BaH.RakNet

type ServerPacket

	function create:ServerPacket(servpeer:ServerPeer, packet:TRKPacket)
	
		if (not packet) ..
			return null;
		
		local sp:ServerPacket = new ServerPacket;
		
		sp.m_data = string.FromCString(packet.GetData());
		sp.m_msgType = packet.GetPacketIdentifier();
		sp.m_senderAddr = packet.GetSystemAddress();
		sp.m_senderPort = sp.m_senderAddr.GetPort();
		sp.m_senderIndex = servpeer.m_peer.GetIndexFromSystemAddress(sp.m_senderAddr);
		
		return sp;
	
	end function
	
	field m_data:string;
	field m_msgType:int;
	field m_senderAddr:TRKSystemAddress;
	field m_senderPort:string;
	field m_senderIndex:int;

end type

type ServerPeer

	function create:ServerPeer(maxConnections:int, threadSleepTimer:short, port:short)
	
		local s:ServerPeer= new ServerPeer;
		
		s.m_peer = TRKRakNetworkFactory.GetRakPeerInterface();
		s.m_socketDescr = TRKSocketDescriptor.CreateSocketDescriptor(port);
		s.m_sysaddr = s.m_peer.GetInternalID(UNASSIGNED_SYSTEM_ADDRESS);
		s.m_port = port;
		
		s.m_peer.Startup(maxConnections, threadSleepTimer, s.m_socketDescr);
		s.m_peer.SetMaximumIncomingConnections(maxConnections);
		
		return s;
	
	end function
	
	method destroy()
	
		m_peer.Shutdown(300);
		TRKRakNetworkFactory.DestroyRakPeerInterface(m_peer);
	
	end method
	
	method receive:ServerPacket()
	
		return ServerPacket.create(self, m_peer.Receive());
	
	end method
	
	method kick(addr:TRKSystemAddress)
	
		m_peer.CloseConnection(addr, 1);
	
	end method
	
	field m_peer:TRKRakPeerInterface;
	field m_socketDescr:TRKSocketDescriptor;
	field m_sysaddr:TRKSystemAddress;
	field m_port:short;

end type
